home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / dev / c / TinyGL.lha / tinygl / src / glu.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-04  |  2.0 KB  |  92 lines

  1. #include <math.h>
  2. #include <GL/glu.h>
  3.  
  4. void gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez,
  5.                     GLdouble centerx, GLdouble centery, GLdouble centerz,
  6.                     GLdouble upx, GLdouble upy, GLdouble upz) {
  7.    GLfloat m[16];
  8.    GLfloat x[3], y[3], z[3];
  9.    GLfloat mag;
  10.  
  11.    /* Make rotation matrix */
  12.  
  13.    /* Z vector */
  14.    z[0] = eyex - centerx;
  15.    z[1] = eyey - centery;
  16.    z[2] = eyez - centerz;
  17.    mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
  18.    if (mag) {            /* mpichler, 19950515 */
  19.       z[0] /= mag;
  20.       z[1] /= mag;
  21.       z[2] /= mag;
  22.    }
  23.  
  24.    /* Y vector */
  25.    y[0] = upx;
  26.    y[1] = upy;
  27.    y[2] = upz;
  28.  
  29.    /* X vector = Y cross Z */
  30.    x[0] = y[1] * z[2] - y[2] * z[1];
  31.    x[1] = -y[0] * z[2] + y[2] * z[0];
  32.    x[2] = y[0] * z[1] - y[1] * z[0];
  33.  
  34.    /* Recompute Y = Z cross X */
  35.    y[0] = z[1] * x[2] - z[2] * x[1];
  36.    y[1] = -z[0] * x[2] + z[2] * x[0];
  37.    y[2] = z[0] * x[1] - z[1] * x[0];
  38.  
  39.    /* mpichler, 19950515 */
  40.    /* cross product gives area of parallelogram, which is < 1.0 for
  41.     * non-perpendicular unit-length vectors; so normalize x, y here
  42.     */
  43.    mag = sqrt(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
  44.    if (mag) {
  45.       x[0] /= mag;
  46.       x[1] /= mag;
  47.       x[2] /= mag;
  48.    }
  49.  
  50.    mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
  51.    if (mag) {
  52.       y[0] /= mag;
  53.       y[1] /= mag;
  54.       y[2] /= mag;
  55.    }
  56.  
  57. #define M(row,col)  m[col*4+row]
  58.    M(0, 0) = x[0];
  59.    M(0, 1) = x[1];
  60.    M(0, 2) = x[2];
  61.    M(0, 3) = 0.0;
  62.    M(1, 0) = y[0];
  63.    M(1, 1) = y[1];
  64.    M(1, 2) = y[2];
  65.    M(1, 3) = 0.0;
  66.    M(2, 0) = z[0];
  67.    M(2, 1) = z[1];
  68.    M(2, 2) = z[2];
  69.    M(2, 3) = 0.0;
  70.    M(3, 0) = 0.0;
  71.    M(3, 1) = 0.0;
  72.    M(3, 2) = 0.0;
  73.    M(3, 3) = 1.0;
  74. #undef M
  75.    glMultMatrixf(m);
  76.  
  77.    /* Translate Eye to Origin */
  78.    glTranslatef(-eyex, -eyey, -eyez);
  79. }
  80.  
  81.  
  82. void gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat znear, GLfloat zfar) {
  83.    GLfloat xmin, xmax, ymin, ymax;
  84.  
  85.    ymax = znear * tan(fovy * 0.008726646);
  86.    ymin = -ymax;
  87.    xmin = ymin * aspect;
  88.    xmax = ymax * aspect;
  89.  
  90.    glFrustum(xmin, xmax, ymin, ymax, znear, zfar);
  91. }
  92.